package logic

import (
	"context"
	"fmt"
	"userrpc/internal/svc"
	"userrpc/models/mysql"
	"userrpc/pkg"
	"userrpc/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealnameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealnameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealnameLogic {
	return &RealnameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealnameLogic) Realname(in *user.RealnameRequest) (*user.RealnameResponse, error) {
	// todo: add your logic here and delete this line
	r := mysql.User{}
	err := r.TableName(int(in.Userid), in.Idcard)
	if err != nil {
		return nil, fmt.Errorf("实名失败")
	}
	if r.ID > 0 {
		return nil, fmt.Errorf("实名已存在")
	}
	pkg.TengXunYun(in.Reanlname, in.Idcard)
	r.Realname = in.Reanlname
	r.Userid = int(in.Userid)
	r.Idcard = in.Idcard
	err = r.Create()
	if err != nil {
		return nil, fmt.Errorf("添加成功")
	}
	return &user.RealnameResponse{}, nil
}
